﻿using MySqlConnector;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExcelToData
{
    internal class MySqlHelper
    {
        private static string connectionString = "Data Source =(local); Initial Catalog = Test; User ID = sa; Password=sa";
        public MySqlHelper()
        {
            connectionString = AppCode.connectionString;
        }

        public static Boolean IsConNet()
        {
            bool Flag = false;
            using (MySqlConnection conn = new MySqlConnection(connectionString))
            {
                try
                {
                    conn.Open();
                    if (conn.State == ConnectionState.Open)
                    {
                        Flag = true;
                    }
                }
                catch
                {
                    Flag = false;
                }
            }
            return Flag;
        }



        public static Boolean IsExistTable(string TableName)
        {
            using (MySqlConnection conn = new MySqlConnection(connectionString))
            {
                if (conn.State != ConnectionState.Open) conn.Open();

                //string isExitTableSql = @"select * from dbo.sysobjects where name = '" + TableName + @"'";
                string isExitTableSql = @"select * from dbo.sysobjects where id = object_id(N'" + TableName + "') and (type='U' or type='S')";
                MySqlCommand cmd = new MySqlCommand(isExitTableSql, conn);
                object pResult = cmd.ExecuteScalar();
                if (pResult == null || pResult == System.DBNull.Value) return false;
                return true;
            }
        }

        public static Boolean ExeCuteSql(String strSql)
        {
            bool Flag = false;
            using (MySqlConnection conn = new MySqlConnection(connectionString))
            {
                using (MySqlCommand cmd = new MySqlCommand(strSql, conn))
                {
                    conn.Open();
                    cmd.CommandType = System.Data.CommandType.Text;
                    cmd.ExecuteNonQuery();
                    conn.Close();
                    Flag = true;
                }
            }
            return Flag;
        }
    }
}
